﻿using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace FwProject.Comm.ExcelUtil
{
    public class ExcExcelImportelUtil<T> where T : new()
    {
        private static IWorkbook workbook = null;
        public static List<T> GetExcelToList(string excelfileName)
        {
            List<T> list = new List<T>();
            ISheet sheet = null;
            // PropertyInfo propertyInfo = null;
            int startRow = 0;
            using (FileStream fs = new FileStream(excelfileName, FileMode.Open, FileAccess.Read))
            {
                workbook = new XSSFWorkbook(fs);
                sheet = workbook.GetSheetAt(0);
                if (sheet != null)
                {

                    //Parallel
                    IRow firstRow = sheet.GetRow(0);//第一行
                    int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数
                    startRow = sheet.FirstRowNum + 1;
                    for (int j = startRow; j <= sheet.LastRowNum; j++)
                    {
                        IRow RowValue = sheet.GetRow(j);//数据行

                        T tmodel = new T();
                        Type type = tmodel.GetType();
                        for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                        {
                            try
                            {

                                PropertyInfo propertyInfo = null;
                                ICell cell = firstRow.GetCell(i);
                                foreach (var item in type.GetProperties())
                                {
                                    var displayName = item.GetCustomAttribute<DisplayAttribute>();
                                    if (displayName != null)
                                    {
                                        if (displayName.Name == cell.StringCellValue)
                                            propertyInfo = type.GetProperty(item.Name);
                                    }
                                }

                                if (propertyInfo == null)
                                    propertyInfo = type.GetProperty(cell.StringCellValue);

                                if (propertyInfo.PropertyType == typeof(System.String))
                                {
                                    propertyInfo.SetValue(tmodel, RowValue.GetCell(i).ToString(), null);
                                }
                                else if (propertyInfo.PropertyType == typeof(System.Decimal))
                                {
                                    propertyInfo.SetValue(tmodel, Convert.ToDecimal(GetCellValue(RowValue.GetCell(i))), null);
                                }
                                else if (propertyInfo.PropertyType == typeof(System.Int32))
                                {
                                    propertyInfo.SetValue(tmodel, Convert.ToInt32(GetCellValue(RowValue.GetCell(i))), null);
                                }
                                else if (propertyInfo.PropertyType == typeof(System.Double))
                                {
                                    propertyInfo.SetValue(tmodel, Convert.ToDouble(GetCellValue(RowValue.GetCell(i))), null);
                                }
                                else if (propertyInfo.PropertyType == typeof(System.DateTime))
                                {
                                    propertyInfo.SetValue(tmodel, Convert.ToDateTime(GetCellValue(RowValue.GetCell(i))), null);
                                }
                                else
                                {
                                    // RowValue.GetCell(i).SetCellType(CellType.String);
                                    propertyInfo.SetValue(tmodel, RowValue.GetCell(i).ToString(), null);
                                }



                            }
                            catch (Exception e)
                            {

                                new ArgumentOutOfRangeException("Excel导入失败", e);
                            }

                        }
                        list.Add(tmodel);
                    }

                    //Parallel.For(startRow, sheet.LastRowNum, () => {


                    //});
                }
            }
            return list;
        }
        public static object GetCellValue(ICell cell)
        {
            object value = null;
            try
            {
                if (cell.CellType != CellType.Blank)
                {
                    switch (cell.CellType)
                    {
                        case CellType.Numeric:
                            // Date Type的数据CellType是Numeric
                            if (DateUtil.IsCellDateFormatted(cell))
                            {
                                value = cell.DateCellValue;
                            }
                            else
                            {
                                // Numeric type
                                value = cell.NumericCellValue;
                            }
                            break;
                        case CellType.Boolean:
                            // Boolean type
                            value = cell.BooleanCellValue;
                            break;
                        default:
                            // String type
                            value = cell.StringCellValue;
                            break;
                    }
                }
            }
            catch (Exception)
            {
                value = "";
            }

            return value;
        }
    }
}
